import {reqGetDownload, reqUploadFile} from "@/api/file"
import {requestGetData, requestIsSuccess} from "@/api/request";

class MyUploadAdapter {
    constructor(loader) {
        this.loader = loader;
    }

    // 启动上载过程
    upload() {
        return this.loader.file
            .then(file => new Promise((resolve, reject) => {
                var formdata = new FormData();
                formdata.append("file", file);
       /*         const formdata={
                    file:file
                }*/
                reqUploadFile(formdata).then((res) => {
                    if (requestIsSuccess(res)) {
                        //上传成功，从后台获取图片的url地址
                        resolve({
                            default: requestGetData(res)
                        });
                    }
                });
            })).catch(error=>{
                console.log('error:'+error)
            });
    }

    // 中止上载过程
    abort() {
        if (this.xhr) {
            this.xhr.abort();
        }
    }
}

function MyCustomUploadAdapterPlugin(editor) {
    editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
        // 在这里将URL配置为后端上载脚本
        return new MyUploadAdapter(loader);
    };
}

export {
    MyUploadAdapter,
    MyCustomUploadAdapterPlugin
}